var requestHandler = {
  url: "",
  header: "",
  params: {},
  success: function (res) {
    // success
  },
  fail: function () {
    // fail
  },
}



//GET请求
function GET(requestHandler) {
  request('GET', requestHandler)
}
//POST请求
function POST(requestHandler) {
  request('POST', requestHandler)
}

function request(method, requestHandler, _app) {
  //注意：可以对params加密等处理
  const app = getApp() || _app;
  const params = requestHandler.params;
  const url = requestHandler.url;
  const loginInfo = (app && app.globalData && app.globalData.loginInfo) || wx.getStorageSync('loginInfo');
  const header = {
    'content-type': 'application/x-www-form-urlencoded',
    Accept: 'application/json',
    ...requestHandler.header
  }
  if (!requestHandler.noNeedToken && loginInfo && loginInfo.token) {
    header["User-Token"] = loginInfo.token;
  }
  if (!requestHandler.noNeedCookie && loginInfo && loginInfo.cookie) {
    header["Cookie"] = loginInfo.cookie;
  }
  wx.request({
    url: app.globalData.api_url + url,
    data: params,
    method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: header, // 设置请求的 header
    success: function (res) {
      //注意：可以对参数解密等处理
      requestHandler.success(res)
    },
    fail: function () {
      requestHandler.fail()
    },
    complete: function () {
      // complete
    }
  })
}

module.exports = {
  GET: GET,
  POST: POST
}